{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "94cbfdf3",
   "metadata": {},
   "source": [
    "## 01. 字符串转字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c680f60",
   "metadata": {},
   "source": [
    "老师我想把这样的字符串“1=123 2=234”转变成这样的“1\"：\"123“,\" 2\":\"234”列表应该怎么写？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8a7f27ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1=123 2=234'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = \"1=123 2=234\"\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "316ac8ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1=123', '2=234']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用空格拆分成多组Key=value\n",
    "datas = data.split()\n",
    "datas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e257a57f",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = {}\n",
    "# 对每个元素，用等号分割，存入字典\n",
    "for keyvalue in datas:\n",
    "    key,value = keyvalue.split(\"=\")\n",
    "    result[key]=value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a4fc8a25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'1': '123', '2': '234'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f8122a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
